﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IvyBus;

namespace IvyManager
{
    class IvySender
    {
        Ivy bus;
        String address;
        bool isRunning = false;

        public IvySender(String busAddress, String msgInit)
        {
            bus = new Ivy("KinectSender", msgInit);
            this.address = busAddress;
        }

        public void start()
        {
            bus.Start(this.address);
            isRunning = true;
        }

        public void stop()
        {
            try
            {
                isRunning = false;
                //bus.stop();
                Console.WriteLine("Sender arrêté");
            }
            catch
            {
                Console.WriteLine("Problème arrêt Ivy");
            }
        }

        /*public void setBusAddress(String newBusAddress)
        {
            this.address = newBusAddress;
        }

        public String getBusAddress()
        {
            if (this.address == null) return "null";
            else return this.address;
        }*/

        public bool sendMsg(String m) 
        {
            if (isRunning)
            {
                try
                {
                    bool res = (bus.SendMsg(m) > 0);
                    return res;
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine("Plus de client pour IvySender : \r\n" + ioe.Message + "\r\n");
                    return false;
                }
            }
            else return false;
        }
    }
}
